#!/usr/bin/env python
# -*- coding:utf-8 -*-

# file:count_timeit_decorator.py
# author:张世航
# datetime:2024/11/10 17:11
# software: PyCharm
"""
this is function  description 
"""
# import module your need

# 4  编写装饰器来实现，对目标函数进行装饰,计算函数的运行耗时，
#     分3种情况：目标函数无参数无返回值，目标函数有参数，目标函数有返回值；
#      三个目标函数分别为：
#      A 打印输出20000之内的素数；
#      B 计算整数2-10000之间的素数的个数；
#      C 计算整数2-M之间的素数的个数

import time
import functools

def timeit_decorator(func):
    @functools.wraps(func)
    def wrapper(*args, **kwargs):
        start_time = time.time()
        if not args and not kwargs:  # 无参数无返回值
            result = func()
        elif not kwargs:  # 有参数无返回值或返回值
            result = func(*args)
        else:  # 有参数有返回值
            result = func(*args, **kwargs)
        end_time = time.time()
        print(f"Function {func.__name__} took {end_time - start_time:.4f} seconds")
        return result
    return wrapper

# A: 打印输出20000之内的素数
@timeit_decorator
def print_primes_up_to_20000():
    for num in range(2, 20001):
        if all(num % i != 0 for i in range(2, int(num**0.5) + 1)):
            print(num)

# B: 计算整数2-10000之间的素数的个数
@timeit_decorator
def count_primes_up_to_10000():
    return sum(1 for num in range(2, 10001) if all(num % i != 0 for i in range(2, int(num**0.5) + 1)))

# C: 计算整数2-M之间的素数的个数
@timeit_decorator
def count_primes_up_to_M(M):
    return sum(1 for num in range(2, M + 1) if all(num % i != 0 for i in range(2, int(num**0.5) + 1)))

# 示例调用
print_primes_up_to_20000()
print(count_primes_up_to_10000())
print(count_primes_up_to_M(5000))